﻿using System;
using System.Threading.Tasks;

namespace Ecobee.Net.Demo
{
	public abstract class CommandBase : ICommand
	{
		private string _name = string.Empty;
		private string _key = string.Empty;

		public CommandBase(string key, string name)
		{
			this.Key = key;
			this.Name = name;
		}

		public virtual string Name
		{
			get
			{
				return _name;
			}
			set
			{
				_name = value;
			}
		}

		public virtual string Key
		{
			get
			{
				return _key;
			}
			set
			{
				_key = value;
			}
		}

		public async virtual Task<bool> Execute(Api api)
		{
			bool returnValue = false;

			try
			{
				Console.WriteLine();
				Console.WriteLine("Starting command '{0}'.", this.Name);
				returnValue = await this.OnExcute(api);

				if (returnValue)
				{
					Console.WriteLine("Command completed successfully.", this.Name);
				}
				else
				{
					Console.WriteLine("Command failed.", this.Name);
				}
			}
			catch (Exception ex)
			{
				Console.ForegroundColor = ConsoleColor.Red;
				Console.WriteLine(ex.Message);
				Console.ResetColor();
			}
			finally
			{
				Console.WriteLine();
			}

			return returnValue;
		}

		protected virtual Task<bool> OnExcute(Api api)
		{
			throw new NotImplementedException();
		}
	}
}
